package gui.search;

import java.awt.Component;
import java.util.Collections;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.text.JTextComponent;


public class ComboBoxC extends JComboBox {
    private EditorComboBoxAutoC ecbac;
    private JTextComponent editor;
    private Vector<String> data;

    public ComboBoxC() {
	super();
	setEditable(true);
	data = new Vector<String>();
	Collections.sort(data);
	setModel(new DefaultComboBoxModel(data));
	ecbac = new EditorComboBoxAutoC(this);
	editor = (JTextComponent) this.getEditor().getEditorComponent();
	editor.setDocument(ecbac);

	setRenderer(new MyComboBoxRenderer(data));
    }

    public ComboBoxC(Vector<String> vect) {

	super();
	setEditable(true);
	data = vect;
	Collections.sort(data);
	setModel(new DefaultComboBoxModel(data));
	ecbac = new EditorComboBoxAutoC(this);
	editor = (JTextComponent) this.getEditor().getEditorComponent();
	editor.setDocument(ecbac);
	setRenderer(new MyComboBoxRenderer(data));

    }

    public void changeData(Vector<String> vect) {
	data = vect;
	setModel(new DefaultComboBoxModel(data));
	ecbac = new EditorComboBoxAutoC(this);
	editor = (JTextComponent) this.getEditor().getEditorComponent();
	editor.setDocument(ecbac);
	setRenderer(new MyComboBoxRenderer(data));

    }

}

class MyComboBoxRenderer extends BasicComboBoxRenderer {

    private Vector<String> tooltips;

    public MyComboBoxRenderer(Vector<String> t) {
	super();
	tooltips = t;
    }

    public Component getListCellRendererComponent(JList list, Object value,
	    int index, boolean isSelected, boolean cellHasFocus) {
	if (isSelected) {
	    setBackground(list.getSelectionBackground());
	    setForeground(list.getSelectionForeground());
	    if (-1 < index) {
		list.setToolTipText(tooltips.get(index));

	    }
	} else {
	    setBackground(list.getBackground());
	    setForeground(list.getForeground());
	}
	setFont(list.getFont());
	setText((value == null) ? "" : value.toString());
	return this;
    }
}
